package com.ddyx.common;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.ddyx.monitor.entity.DomainServer;
import com.ddyx.monitor.entity.DomainServerLogsVo;
import com.ddyx.monitor.entity.ResultMap;

public class HttpHandle {

	/**
	 * <p>Title: doPostHandle</p>
	 * <p>Description: 
	 * 		post请求
	 * </p>
	 * @param ds
	 * @return
	 */
	public ResultMap doPostHandle(DomainServer ds) {
		ResultMap resultMap=new ResultMap();
		resultMap.setCode(6000);
		resultMap.setMsg("返回值为空");
		String urlStr=ds.getServerName()+":"+ds.getPort()+ds.getMethodName();
		resultMap.setRequestTime(new Date());
		String resultStr=HttpUtils.doPost(urlStr, null, null);
		if(StringUtils.isNotBlank(resultStr) && !"null".equals(resultStr)) {
			JSONObject result = JSONObject.parseObject(resultStr);
			int code=result.getIntValue("code");
			String msg=result.getString("msg");
			resultMap.setCode(code);
			resultMap.setMsg(msg);
		}
		return resultMap;
	}

	/**
	 * <p>Title: doEmailHtmlHandle</p>
	 * <p>Description: 
	 * 		发送email(不带附件)
	 * </p>
	 * @param list
	 * @param emailStr
	 * @throws Exception 
	 */
	public void doEmailHtmlHandle(List<DomainServerLogsVo> list,String emailStr) throws Exception {
		String val="";
		for (int i = 0; i < list.size(); i++) {
			DomainServerLogsVo vo=list.get(i);
			val+="<tr><td>"+vo.getServerName()+"</td>"
					+ "<td>"+vo.getDesc()+"</td>"
					+ "<td>"+DateUtil.format_ymd_hms.format(vo.getRequestTime())+"</td>"
					+ "<td>"+vo.getCode()+"</td>"
					+ "<td>"+vo.getMsg()+"</td></tr>";
		}
		String content="<div>"
				+ "<table border=\"1\">"
				+ "<tr>"
				+ "<th>服务器域名</th>"
				+ "<th>项目</th>"
				+ "<th>请求时间</th>"
				+ "<th>状态码</th>"
				+ "<th>返回状态</th>"
				+ "</tr>"
				+ val
				+ "</table>"
				+ "备注:<br/>code:1000=>正常<br/>"
				+ "</div>";

    	EmailUtil emailUtil = EmailUtil.getInstance();
        EmailInfo info = new EmailInfo();
        info.setNotifyTo(emailStr);
        info.setSubject("项目异常预警");
        info.setContent(content);
        //以html形式展示
        emailUtil.sendHtmlMail(info);
	}
	
}
